<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="deviceType">
        <el-select v-model="queryParams.deviceType" placeholder="请选择设备类型" clearable>
          <el-option
            v-for="dict in dict.type.device_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工站" prop="workstation">
        <el-input
          v-model="queryParams.workstation"
          placeholder="请输入工站"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="IP 地址" prop="ipAddress">
        <el-input
          v-model="queryParams.ipAddress"
          placeholder="请输入IP 地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="是否启动" prop="isStarted">
        <el-select v-model="queryParams.isStarted" placeholder="请选择是否启动" clearable>
          <el-option
            v-for="dict in dict.type.yes_or_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否连接" prop="isConnected">
        <el-select v-model="queryParams.isConnected" placeholder="请选择是否连接" clearable>
          <el-option
            v-for="dict in dict.type.yes_or_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否异常" prop="isFaulty">
        <el-select v-model="queryParams.isFaulty" placeholder="请选择是否异常" clearable>
          <el-option
            v-for="dict in dict.type.yes_or_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="零部件设备" prop="isComponentDevice">
        <el-select v-model="queryParams.isComponentDevice" placeholder="请选择是否零部件设备" clearable>
          <el-option
            v-for="dict in dict.type.yes_or_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="副码追溯" prop="subBarcodeTraceability">
        <el-select v-model="queryParams.subBarcodeTraceability" placeholder="请选择副码追溯" clearable>
          <el-option
            v-for="dict in dict.type.sub_barcode_traceability"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:device:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:device:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:device:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:device:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!-- <el-table-column label="设备ID" align="center" prop="id"/> -->
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="设备名称" align="center" prop="deviceName" width="280">
        <template v-slot="scope">
          <router-link :to="'/production/device/config/' + scope.row.id" class="link-type">
            <span>{{ scope.row.deviceName }}</span>
          </router-link>
        </template>
      </el-table-column>

      <el-table-column label="设备类型" align="center" prop="deviceType">
        <template v-slot="scope">
          <dict-tag :options="dict.type.device_type" :value="scope.row.deviceType"/>
        </template>
      </el-table-column>
      <el-table-column label="plc协议" align="center" prop="plcAgreement">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.plc_agreement" :value="scope.row.plcAgreement"/>
        </template>
      </el-table-column>

      <el-table-column label="工站" align="center" prop="workstation"/>
      <el-table-column label="IP 地址" align="center" prop="ipAddress" width="150">
        <template v-slot="scope">
          <router-link :to="'/addressmonitoring/' + scope.row.id" class="link-type">
            <span>{{ scope.row.ipAddress }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="端口号" align="center" prop="port"/>
      <el-table-column label="是否启动" align="center" prop="isStarted">
        <template v-slot="scope">
          <el-switch
            v-model="scope.row.isStarted"
            :active-value="1"
            :inactive-value="0"
            @change="handleStartedStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
     <!-- <el-table-column label="状态地址" align="center" prop="statusAddress" /> -->
      
     <el-table-column label="设备状态" align="center" prop="deviceStatus" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_status" :value="scope.row.deviceStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="是否连接" align="center" prop="isConnected">
        <template v-slot="scope">
          <dict-tag :options="dict.type.yes_or_no" :value="scope.row.isConnected"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="是否异常" align="center" prop="isFaulty">
        <template v-slot="scope">
          <dict-tag :options="dict.type.yes_or_no" :value="scope.row.isFaulty"/>
        </template>
      </el-table-column> -->
      <el-table-column label="换型校验" align="center" prop="changeVerification">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_or_no" :value="scope.row.changeVerification"/>
        </template>
      </el-table-column>
      <el-table-column label="零部件设备" align="center" prop="isComponentDevice">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_or_no" :value="scope.row.isComponentDevice"/>
        </template>
      </el-table-column>
      <el-table-column label="副码追溯" align="center" prop="subBarcodeTraceability">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sub_barcode_traceability" :value="scope.row.subBarcodeTraceability"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="冷却时间(分钟)" align="center" prop="coolingTime" width="180"/> -->
      <!-- <el-table-column label="备注" align="center" prop="remark"/> -->
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="180">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:device:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:device:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备，工厂中的设备信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="plc协议" prop="plcAgreement">
          <el-select v-model="form.plcAgreement" placeholder="请选择plc协议">
            <el-option
              v-for="dict in dict.type.plc_agreement"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称"/>
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceType">
          <el-select v-model="form.deviceType" placeholder="请选择设备类型">
            <el-option
              v-for="dict in dict.type.device_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工站" prop="workstation">
          <el-input v-model="form.workstation" placeholder="请输入工站"/>
        </el-form-item>
        <el-form-item label="冷却时间" prop="coolingTime">
          <el-input v-model="form.coolingTime" placeholder="请输入冷却时间(分钟)" />
        </el-form-item>
        <el-form-item label="IP 地址" prop="ipAddress">
          <el-input v-model="form.ipAddress" placeholder="请输入IP 地址"/>
        </el-form-item>
        <el-form-item label="端口号" prop="port">
          <el-input v-model="form.port" placeholder="请输入端口号"/>
        </el-form-item>
        <el-form-item label="换型校验" prop="changeVerification">
          <el-select v-model="form.changeVerification" placeholder="请选择换型校验">
            <el-option
              v-for="dict in dict.type.yes_or_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="零部件设备" prop="isComponentDevice">
          <el-select v-model="form.isComponentDevice" placeholder="请选择是否零部件设备">
            <el-option
              v-for="dict in dict.type.yes_or_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="副码追溯" prop="subBarcodeTraceability">
          <el-select v-model="form.subBarcodeTraceability" placeholder="请选择副码追溯">
            <el-option
              v-for="dict in dict.type.sub_barcode_traceability"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listDevice, getDevice, delDevice, addDevice, updateDevice, changeStartedStatus} from "@/api/mes/device";

export default {
  name: "Device",
  dicts: ['yes_or_no', 'device_type', 'plc_agreement','device_status', 'sub_barcode_traceability'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备，工厂中的设备信息表格数据
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        deviceName: null,
        deviceType: null,
        workstation: null,
        ipAddress: null,
        isStarted: null,
        isConnected: null,
        isFaulty: null,
        isComponentDevice: null,
        subBarcodeTraceability: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceName: [
          {required: true, message: "设备名称不能为空", trigger: "blur"}
        ],
        plcAgreement: [
          {required: true, message: "PLC协议不能为空", trigger: "change"}
        ],
        deviceType: [
          {required: true, message: "设备类型不能为空", trigger: "change"}
        ],
        workstation: [
          {required: true, message: "工站不能为空", trigger: "blur"}
        ],
        ipAddress: [
          {required: true, message: "IP 地址不能为空", trigger: "blur"}
        ],
        port: [
          {required: true, message: "端口号不能为空", trigger: "blur"}
        ],


      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleStartedStatusChange(row) {
      let text = row.isStarted === 1 ? "启动" : "停止";
      this.$modal.confirm(`确认要 ${text} ${row.deviceName}吗？`).then(function () {

        const data = {
          id: row.id,
          isStarted: row.isStarted
        }
        return changeStartedStatus(data);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
        this.getList()
      }).catch(function () {
        row.isStarted = row.isStarted === "0" ? "1" : "0";
      });
    },
    /** 查询设备，工厂中的设备信息列表 */
    getList() {
      this.loading = true;
      listDevice(this.queryParams).then(response => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deviceName: null,
        deviceType: null,
        plcAgreement: null,
        workstation: null,
        ipAddress: null,
        changeVerification:0,
        port: null,
        isStarted: null,
        isConnected: null,
        isFaulty: null,
        isComponentDevice: 0, // 默认为否
        subBarcodeTraceability: 0, // 默认为不追溯
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDevice(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDevice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDevice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除设备，设备信息编号为"' + ids + '"的数据项？').then(function () {
        return delDevice(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/device/export', {
        ...this.queryParams
      }, `device_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
